{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From d:\\software_install\\miniconda3\\envs\\pytool\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:262: extract_images (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use tf.data to implement this functionality.\n",
      "Extracting .\\train-images-idx3-ubyte.gz\n",
      "WARNING:tensorflow:From d:\\software_install\\miniconda3\\envs\\pytool\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:267: extract_labels (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use tf.data to implement this functionality.\n",
      "Extracting .\\train-labels-idx1-ubyte.gz\n",
      "WARNING:tensorflow:From d:\\software_install\\miniconda3\\envs\\pytool\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:110: dense_to_one_hot (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use tf.one_hot on tensors.\n",
      "Extracting .\\t10k-images-idx3-ubyte.gz\n",
      "Extracting .\\t10k-labels-idx1-ubyte.gz\n",
      "WARNING:tensorflow:From d:\\software_install\\miniconda3\\envs\\pytool\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\mnist.py:290: DataSet.__init__ (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use alternatives such as official/mnist/dataset.py from tensorflow/models.\n",
      "Extracting .\\train-images-idx3-ubyte.gz\n",
      "Extracting .\\train-labels-idx1-ubyte.gz\n",
      "Extracting .\\t10k-images-idx3-ubyte.gz\n",
      "Extracting .\\t10k-labels-idx1-ubyte.gz\n",
      "[[0. 0. 0. 0. 0. 0. 0. 1. 0. 0.]\n",
      " [0. 0. 0. 1. 0. 0. 0. 0. 0. 0.]]\n",
      "[7 3]\n",
      "WARNING:tensorflow:From <ipython-input-2-a5d5458fdcdb>:28: softmax_cross_entropy_with_logits (from tensorflow.python.ops.nn_ops) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "\n",
      "Future major versions of TensorFlow will allow gradients to flow\n",
      "into the labels input on backprop by default.\n",
      "\n",
      "See `tf.nn.softmax_cross_entropy_with_logits_v2`.\n",
      "\n",
      "WARNING:tensorflow:From <ipython-input-2-a5d5458fdcdb>:34: arg_max (from tensorflow.python.ops.gen_math_ops) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Use `tf.math.argmax` instead\n",
      "##########\n",
      "step [100], entropy loss [1.1552749872207642]\n",
      "0.9375\n",
      "0.7871\n",
      "##########\n",
      "step [200], entropy loss [0.8323810696601868]\n",
      "0.90625\n",
      "0.8432\n",
      "##########\n",
      "step [300], entropy loss [1.0119839906692505]\n",
      "0.84375\n",
      "0.8686\n",
      "##########\n",
      "step [400], entropy loss [0.7052441835403442]\n",
      "0.9375\n",
      "0.8666\n",
      "##########\n",
      "step [500], entropy loss [0.7393864393234253]\n",
      "0.90625\n",
      "0.8676\n",
      "##########\n",
      "step [600], entropy loss [0.41435784101486206]\n",
      "0.96875\n",
      "0.8631\n",
      "##########\n",
      "step [700], entropy loss [0.4009144902229309]\n",
      "1.0\n",
      "0.8715\n",
      "##########\n",
      "step [800], entropy loss [1.1557745933532715]\n",
      "0.8125\n",
      "0.8582\n",
      "##########\n",
      "step [900], entropy loss [0.9269925355911255]\n",
      "0.96875\n",
      "0.8271\n",
      "##########\n",
      "step [1000], entropy loss [0.6074692010879517]\n",
      "0.9375\n",
      "0.8915\n",
      "##########\n",
      "step [1100], entropy loss [0.5873085260391235]\n",
      "0.84375\n",
      "0.9026\n",
      "##########\n",
      "step [1200], entropy loss [0.32761698961257935]\n",
      "0.90625\n",
      "0.8972\n",
      "##########\n",
      "step [1300], entropy loss [0.5178085565567017]\n",
      "0.875\n",
      "0.9005\n",
      "##########\n",
      "step [1400], entropy loss [0.2453862428665161]\n",
      "0.96875\n",
      "0.8988\n",
      "##########\n",
      "step [1500], entropy loss [0.08568678796291351]\n",
      "1.0\n",
      "0.8989\n",
      "##########\n",
      "step [1600], entropy loss [0.5298088788986206]\n",
      "0.9375\n",
      "0.898\n",
      "##########\n",
      "step [1700], entropy loss [0.07188793271780014]\n",
      "1.0\n",
      "0.9006\n",
      "##########\n",
      "step [1800], entropy loss [0.05001267045736313]\n",
      "1.0\n",
      "0.8975\n",
      "##########\n",
      "step [1900], entropy loss [0.827202558517456]\n",
      "0.84375\n",
      "0.8994\n",
      "##########\n",
      "step [2000], entropy loss [0.10482095181941986]\n",
      "0.96875\n",
      "0.9024\n",
      "##########\n",
      "step [2100], entropy loss [0.4736775755882263]\n",
      "0.90625\n",
      "0.9028\n",
      "##########\n",
      "step [2200], entropy loss [0.6835545301437378]\n",
      "0.9375\n",
      "0.9023\n",
      "##########\n",
      "step [2300], entropy loss [0.10835442692041397]\n",
      "0.96875\n",
      "0.9024\n",
      "##########\n",
      "step [2400], entropy loss [0.18777060508728027]\n",
      "0.96875\n",
      "0.9009\n",
      "##########\n",
      "step [2500], entropy loss [0.4948887228965759]\n",
      "0.84375\n",
      "0.9\n",
      "##########\n",
      "step [2600], entropy loss [0.6358704566955566]\n",
      "0.84375\n",
      "0.9051\n",
      "##########\n",
      "step [2700], entropy loss [0.19728432595729828]\n",
      "0.90625\n",
      "0.9063\n",
      "##########\n",
      "step [2800], entropy loss [0.2768120765686035]\n",
      "0.875\n",
      "0.9039\n",
      "##########\n",
      "step [2900], entropy loss [0.5315832495689392]\n",
      "0.875\n",
      "0.9049\n",
      "##########\n",
      "step [3000], entropy loss [0.4462539255619049]\n",
      "0.875\n",
      "0.8994\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "from tensorflow.examples.tutorials.mnist import input_data\n",
    "mnist = input_data.read_data_sets('.', one_hot=True)   #one_hot 表示是否展开独热编码的值\n",
    "mnist1 = input_data.read_data_sets('.', one_hot=False)\n",
    "print(mnist.train.labels[:2])\n",
    "print(mnist1.train.labels[:2])  #第1行的第7个数据为1，第二行的第3个数据为1\n",
    "\n",
    "learning_rate = tf.placeholder(tf.float32)\n",
    "\n",
    "#定义单层网络\n",
    "#输入数据\n",
    "#mnist 数据集的图像大小为28*28，应该是二维，但是mnist按照一维进行存储,由于不确定一共有多少数据，第1维因此为None\n",
    "x = tf.placeholder(tf.float32, [None, 784], name='x') \n",
    "#初始权重\n",
    "#tf.truncated_normal()用于从正态分布中截取部分数据生成指定形状的值，mnist输入量为784，输出为10个量\n",
    "W = tf.Variable(tf.truncated_normal([784, 10]),name='weight')  \n",
    "#偏置\n",
    "b = tf.Variable(tf.zeros([10], name='bias'))\n",
    "\n",
    "# 神经网络未经激活的输出\n",
    "logits = tf.matmul(x, W) + b  #matmul矩阵乘法\n",
    "\n",
    "y = tf.placeholder(tf.float32, [None, 10], name='y')\n",
    "\n",
    "#使用交叉熵损失\n",
    "#tf.nn.softmax_cross_entropy_with_logits,进行softmax激活并计算交叉熵，由于是一个批次的数据，因此还需要做下平均\n",
    "cross_entropy = tf.reduce_mean(\n",
    "    tf.nn.softmax_cross_entropy_with_logits(labels=y,logits=logits)) \n",
    "\n",
    "#定义优化器,采用梯度下降优化器\n",
    "train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(cross_entropy)\n",
    "\n",
    "#tf.arg_max()用于获取某一维数据最大值的索引，第二个参数指定了维度\n",
    "correct_prediction = tf.equal(tf.arg_max(y, 1), tf.arg_max(logits, 1))\n",
    "\n",
    "#计算准确率\n",
    "accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))\n",
    "\n",
    "sess = tf.Session()\n",
    "sess.run(tf.global_variables_initializer())\n",
    "\n",
    "lr = 1.0\n",
    "for step in range(3000):\n",
    "    if step > 1000:\n",
    "        lr = 0.3\n",
    "    if step > 2000:\n",
    "        lr = 0.1\n",
    "    batch_x, batch_y = mnist.train.next_batch(32)\n",
    "    _, loss = sess.run([train_step, cross_entropy], feed_dict={x:batch_x, y:batch_y, learning_rate:lr})\n",
    "    if(step+1) % 100 == 0:\n",
    "        print('#' * 10)\n",
    "        print(\"step [{}], entropy loss [{}]\".format(step + 1, loss))\n",
    "        print(sess.run(accuracy, feed_dict={x:batch_x, y:batch_y}))\n",
    "        print(sess.run(accuracy, feed_dict={x:mnist.test.images, y:mnist.test.labels}))\n",
    "        \n",
    "'''\n",
    "训练效果比较差原因：\n",
    "1.训练次数较少\n",
    "2.没有加正则项\n",
    "3.参数初始化不太合理\n",
    "4.仅仅采用了单层网络，模型较为简单\n",
    "5.学习率不合适\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
